package com.jatec.webcopy;

import net.bgreco.DirectoryPicker;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddNewActivity extends Activity {
			

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
             
        setContentView(R.layout.jatec_add_new_dialog);
        
        initView(savedInstanceState);        
    }
    
    private void initView(Bundle bundle){
    	setContentView(R.layout.jatec_add_new_dialog);
    	setTitle("Add New..."); 
    	  	
        Button dir = (Button) findViewById(R.id.dir_button);
        dir.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				chooseDir();
			}
		});
    	
        Button ok = (Button) findViewById(R.id.ok_button);
        ok.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				EditText text = (EditText) findViewById(R.id.name);
				String name = text.getText().toString();
				
				text = (EditText) findViewById(R.id.url);
				String url = text.getText().toString();
				
				text = (EditText) findViewById(R.id.path);
				String localPath = text.getText().toString();
				
				WebItem item = new WebItem();
				item.setName(name);
				item.setUrl(url);
				item.setLocalPath(localPath);
				
				WebItemManager.instance().add(item);
				finish();
			}
		});
        
        Button cancel = (Button) findViewById(R.id.cancel_button);
        cancel.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});    
    }
    
    

	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == DirectoryPicker.PICK_DIRECTORY && resultCode == RESULT_OK) {
			Bundle extras = data.getExtras();
			String path = (String) extras.get(DirectoryPicker.CHOSEN_DIRECTORY);
			// do stuff with path
			System.out.println("selected : " + path);
			
			EditText text = (EditText) findViewById(R.id.path);
			text.setText(path);
		}
	}
	
	private void chooseDir(){
		//main.setBackgroundColor(Color.GREEN);
		Intent intent = new Intent(this, DirectoryPicker.class);
		// optionally set options here
		intent.putExtra(DirectoryPicker.ONLY_DIRS, true);
		intent.putExtra(DirectoryPicker.START_DIR, "/");
		
		startActivityForResult(intent, DirectoryPicker.PICK_DIRECTORY);
	}
}